package com.qersoft.controller;

import com.qersoft.entity.ExamPaper;
import com.qersoft.entity.ExamRule;
import com.qersoft.service.ExamPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/exam-papers")
public class ExamPaperController {

    @Autowired
    private ExamPaperService examPaperService;

    // 生成试卷接口，接收规则ID和试卷名称，根据规则生成试卷
    @PostMapping("generate")
    public ResponseEntity<ExamPaper> generateExamPaper(@RequestParam Integer ruleId, @RequestParam String paperName) {
        ExamRule rule = examPaperService.getExamRuleById(ruleId);
        if (rule == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        ExamPaper generatedPaper = examPaperService.generateExamPaper(rule, paperName);
        return new ResponseEntity<>(generatedPaper, HttpStatus.CREATED);
    }

    // 获取所有试卷列表接口
    @GetMapping
    public ResponseEntity<List<ExamPaper>> getExamPaperList() {
        List<ExamPaper> examPaperList = examPaperService.getExamPaperList();
        return new ResponseEntity<>(examPaperList, HttpStatus.OK);
    }

    // 根据试卷ID获取试卷详情接口
    @GetMapping("/{paperId}")
    public ResponseEntity<ExamPaper> getExamPaperById(@PathVariable Integer paperId) {
        ExamPaper examPaper = examPaperService.getExamPaperById(paperId);
        if (examPaper == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(examPaper, HttpStatus.OK);
    }
}